import java.io.*;
import java.net.*;

import java.util.*;



class RunClass implements Runnable{
    Socket socket = null;
    OutputStream os = null;
    InputStream is = null;  
    public RunClass(Socket socket){
        this.socket = socket;
    }

    public void run(){
        try{
            is = socket.getInputStream();
            os = socket.getOutputStream();

            while (true){
                byte[] b = new byte[1024];
                int n = is.read(b);
                System.out.println("Client Send:\t" + new String(b, 0, n));
                String res="";
                try{
                    res = SocketServerClass.runCmd(new String(b, 0, n));
                }catch (Exception e){
                    e.printStackTrace();
                }
                byte[] bs = res.getBytes();
                os.write(bs, 0, bs.length);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                os.close();
                is.close();
                socket.close();
            } catch (Exception e) {}
        }
    }

}

public class SocketServerClass {
    ServerSocket serverSocket = null;
 
    int port = 10001;
    public void establish() throws Exception{
        serverSocket = new ServerSocket(port);
    }
    public void run() throws Exception{
        this.establish();
        while (true){
            Socket socket = serverSocket.accept();
            RunClass run = new RunClass(socket);
            Thread t = new Thread(run);
            t.start();
        }
    }
    public void start() {
        try {
            run();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                serverSocket.close();
            } catch (Exception e) {}
        }
    }

    public static void main(String argv[]){
        SocketServerClass server = new SocketServerClass();
        server.start();
    }

    public static String runCmd(String cmd){
       Runtime run = Runtime.getRuntime();
       String ret = "";
        try {
            Process p = run.exec(cmd);

            InputStream stderr = p.getErrorStream();
            InputStreamReader isr = new InputStreamReader(stderr);
            BufferedReader br = new BufferedReader(isr);
            String line = null;
            String err = "";
            System.out.println("<ERROR>");
            while((line = br.readLine()) != null){
                System.out.println(line);
                err += "\n" + line;
            }
            System.out.println("</ERROR>");

            BufferedInputStream in = new BufferedInputStream(p.getInputStream());
            BufferedReader inBr = new BufferedReader(new InputStreamReader(in));
            String lineStr;
            
            while ((lineStr = inBr.readLine()) != null)
            {
                System.out.println(lineStr);
                ret += "\n" + lineStr;
            } 
            ret += "<ERROR/> " + err + "</ERROR>";
            if (p.waitFor() != 0) {
                if (p.exitValue() == 1)
                {
                    System.err.println("Cmd Failed.");
                }
            }
            inBr.close();
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if (ret.equals("")) ret = "Serious Error.";
            return ret;
        }
    
    
    }
}
